package com.bhs.sansong.ui.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.utils.MediaStoreUtils;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.WxShareUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.journeyapps.barcodescanner.BarcodeEncoder;


import java.util.HashMap;
import java.util.Map;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 分享邀请
 */
public class InvitationCodeActivity extends BaseActivity implements View.OnClickListener {


    private FrameLayout mFlScan;
    private TextView mTvHint;
    private FrameLayout mFlCode;
    private ImageView mQrCode;
    private TextView mTvSave;
    private TextView mTvShare;
    private String content;
    private String invite_code;
    private TextView mTvRecommend;

    @Override
    public int initLayout() {
        return R.layout.activity_invitation_code;
    }

    @Override
    public void initView() {
        invite_code = getIntent().getStringExtra("invite_code");
        if (SharedPreferencesUtils.getString(mActivity, "urlType").equals("1")) {
            content = "https://saasdemo.sansongkeji.com/adminsite/#/agreement/register?code=" + invite_code;
        } else {
            content = "https://test.sansongkeji.com/adminsite/#/agreement/register?code=" + invite_code;
        }
        mFlScan = findViewById(R.id.fl_scan);
        mTvHint = findViewById(R.id.tv_hint);
        mFlCode = findViewById(R.id.fl_code);
        mQrCode = findViewById(R.id.qrCode);
        mTvSave = findViewById(R.id.tv_save);
        mTvShare = findViewById(R.id.tv_share);
        mTvSave.setOnClickListener(this);
        mTvShare.setOnClickListener(this);
        mTvHint.setText(String.format("邀请码：%s", invite_code));
        mTvRecommend = findViewById(R.id.tv_recommend);
        Spanned PageView = Html.fromHtml("推荐下载并成功认证，可获得<font color=\"#FF6067\">领取红包</font>或<font color=\"#FF6067\">优惠券</font>");
        mTvRecommend.setText(PageView);
    }

    @Override
    public void initData() {
        mQrCode.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= 16) {
                    mQrCode.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mQrCode.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                try {
                    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                    //HashMap设置二维码参数
                    Map map = new HashMap();
                    //   设置容错率 L>M>Q>H  等级越高扫描时间越长,准确率越高
                    map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
                    //设置外边距
                    map.put(EncodeHintType.MARGIN, 1);
                    Bitmap bitmap = barcodeEncoder.encodeBitmap(content, BarcodeFormat.QR_CODE, mQrCode.getWidth(), mQrCode.getHeight(), map);
                    mQrCode.setImageBitmap(bitmap);
                } catch (Exception e) {

                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_save) {
            // 获取图片某布局
            mFlCode.setDrawingCacheEnabled(true);
            mFlCode.buildDrawingCache();
            methodRequiresTwoPermission();
        } else if (view.getId() == R.id.tv_share) {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_invite);
            WxShareUtils.shareWeb(mActivity, content, "邀请下载", "注册成功领取大额新人红包", bmp);
        }
    }

    public static final int RC_LOCATION = 1; // requestCode
    String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @AfterPermissionGranted(RC_LOCATION)
    private void methodRequiresTwoPermission() {
        if (EasyPermissions.hasPermissions(this, perms)) {
            MediaStoreUtils.saveImages(mActivity, mFlCode.getDrawingCache());
            mFlCode.setDrawingCacheEnabled(false);
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "我需要权限来保存图片", RC_LOCATION, perms);
        }
    }
}